//
//  JSMLeavingMoreViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingMoreViewController.h"
#import "JSMLeavingMainAndHotModel.h"
#import "JSMLeavingMoreTableViewCell.h" //
#import "JSMLeavingMainAndHotModel.h"
#import "JSMLeavingUserModel.h"
#import "JSMLeavingMainDetailsViewController.h"
@interface JSMLeavingMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)BaseTableView *leavingMoreTableView;
@property(nonatomic, assign) NSInteger urlPage;
@property(nonatomic, strong) NSMutableArray *moreArr;
@end

@implementation JSMLeavingMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"精品分享";
    self.urlPage = 0;
    [self createLeavingMoreTableView];
    [self createData];
}

- (void)createLeavingMoreTableView {
    self.leavingMoreTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 24) style:UITableViewStylePlain];
    [self.view addSubview:self.leavingMoreTableView];
    self.leavingMoreTableView.dataSource = self;
    self.leavingMoreTableView.delegate = self;
    self.leavingMoreTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
//    self.leavingMoreTableView.separatorColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>]
    
}
- (void)createData {
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", self.urlPage];
    [AFNetworkingTool getUrl:url hudSuperView:self.view result:JSON success:^(id result) {
        if (result != nil) {
            self.moreArr = [NSMutableArray array];
            self.moreArr = [JSMLeavingMainAndHotModel baseModelByArr:result[@"data"][@"hot_spot_list"]];
            if (self.moreArr.count > 0) {
                [self.leavingMoreTableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.moreArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    JSMLeavingMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[JSMLeavingMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    JSMLeavingMainAndHotModel *mainModel = self.moreArr[indexPath.row];
    JSMLeavingUserModel *useModel = mainModel.useModel;
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:useModel.avatar_m] placeholderImage:PlaceHolderImage];
    cell.userNameLabel.text = useModel.name;
    cell.titleLabel.text = mainModel.textStr;
#pragma mark
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:mainModel.index_cover] placeholderImage:PlaceHolderImage];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.leavingMoreTableView deselectRowAtIndexPath:indexPath animated:NO];
    JSMLeavingMainDetailsViewController *leavingMainDetails = [[JSMLeavingMainDetailsViewController alloc] init];
    leavingMainDetails.mainModel = self.moreArr[indexPath.row];
    [self.navigationController pushViewController:leavingMainDetails animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
